﻿/******************************************************************************
 *  All Rights Reserved , Copyright (C) 2012 , EricHu. 
 *  作    者： EricHu
 *  创建时间： 2012-5-25 10:15:58
 ******************************************************************************/
using System;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.ServiceModel;

namespace RDIFramework.ServiceAdapter
{
    using RDIFramework.BusinessEntity;
    using RDIFramework.IService;
    using RDIFramework.Utilities;

    /// <summary>
    /// PlatFormAddInService
    /// 平台插件服务
    /// 
    /// 修改纪录
    /// 
    ///		2012-05-25 版本：1.0 EricHu 建立PlatFormAddInService。
    ///		
    /// 版本：1.0
    ///
    /// <author>
    ///		<name>EricHu</name>
    ///		<date>2012-05-25</date>
    /// </author> 
    /// </summary>
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class PlatFormAddInService : System.MarshalByRefObject, IPlatFormAddInService
    {
        private string serviceName = RDIFrameworkMessage.PlatFormAddInService;

        /// <summary>
        /// .NET快速开发整合框架（RDIFramework.NET）数据库连接
        /// </summary>
        private readonly string RDIFrameworkDbConection = SystemInfo.RDIFrameworkDbConection;

        /// <summary>
        /// 新增平台插件
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="entity">实体</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回状态信息</param>
        /// <returns>主鍵</returns>
        public string Add(UserInfo userInfo, PiPlatFormAddInEntity entity, out string statusCode, out string statusMessage)
        {
            // 寫入調試訊息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加強安全驗證防止未授權匿名調用
            #if (!DEBUG)
				LogOnService.UserIsLogOn(userInfo);
            #endif

            string returnValue = string.Empty;
            statusCode         = string.Empty;            

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPlatFormAddInManager manager = new PiPlatFormAddInManager(dbProvider, userInfo);
                    returnValue   = manager.Add(entity, out statusCode);
                    statusMessage = manager.GetStateMessage(statusCode);
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }               

            // 寫入調試訊息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }

        /// <summary>
        /// 取得平台插件列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>数据表</returns>
        public DataTable GetDT(UserInfo userInfo)
        {
            // 寫入調試訊息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加強安全驗證防止未授權匿名調用
            #if (!DEBUG)
				LogOnService.UserIsLogOn(userInfo);
            #endif

            DataTable dataTable = new DataTable(PiPlatFormAddInTable.TableName);

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 取得列表
                    PiPlatFormAddInManager manager = new PiPlatFormAddInManager(dbProvider, userInfo);
                    dataTable = manager.GetDT(CiDbLinkDefineTable.FieldDeleteMark, 0);
                    dataTable.TableName = PiPlatFormAddInTable.TableName;
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 寫入調試訊息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return dataTable;
        }

        /// <summary>
        /// 取得平台插件实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">主鍵</param>
        /// <returns>实体</returns>
        public PiPlatFormAddInEntity GetEntity(UserInfo userInfo, string id)
        {
            // 寫入調試訊息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加強安全驗證防止未授權匿名調用
            #if (!DEBUG)
				LogOnService.UserIsLogOn(userInfo);
            #endif

            PiPlatFormAddInEntity entity = null;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPlatFormAddInManager manager = new PiPlatFormAddInManager(dbProvider, userInfo);
                    entity = manager.GetEntity(id);
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            // 寫入調試訊息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return entity;
        }

        /// <summary>
        /// 更新平台插件
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="entity">实体</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回状态信息</param>
        /// <returns>影响的行数</returns>
        public int Update(UserInfo userInfo, PiPlatFormAddInEntity entity, out string statusCode, out string statusMessage)
        {
            // 寫入調試訊息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加強安全驗證防止未授權匿名調用
            #if (!DEBUG)
				LogOnService.UserIsLogOn(userInfo);
            #endif

            int returnValue = 0;
            statusCode = string.Empty;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPlatFormAddInManager manager = new PiPlatFormAddInManager(dbProvider, userInfo);
                    returnValue = manager.UpdateEntity(entity);
                    // returnValue = manager.Update(entity, out statusCode);
                    statusMessage = manager.GetStateMessage(statusCode);
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 寫入調試訊息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }

        /// <summary>
        /// 依主键取得平台插件列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主鍵</param>
        /// <returns>数据表</returns>
        public DataTable GetDTByIds(UserInfo userInfo, string[] ids)
        {
            // 寫入調試訊息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加強安全驗證防止未授權匿名調用
            #if (!DEBUG)
				LogOnService.UserIsLogOn(userInfo);
            #endif

            DataTable dataTable = new DataTable(PiPlatFormAddInTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPlatFormAddInManager manager = new PiPlatFormAddInManager(dbProvider, userInfo);
                    dataTable = manager.GetDT(PiPlatFormAddInTable.FieldId, ids);
                    dataTable.TableName = PiPlatFormAddInTable.TableName;
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            // 寫入調試訊息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return dataTable;
        }

        /// <summary>
        /// 按条件获取平台插件数据列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="names">字段</param>
        /// <param name="values">值</param>
        /// <returns>数据表</returns>
        public DataTable GetDTByValues(UserInfo userInfo, string[] names, object[] values)
        {
            // 寫入調試訊息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加強安全驗證防止未授權匿名調用
            #if (!DEBUG)
				LogOnService.UserIsLogOn(userInfo);
            #endif

            DataTable dataTable = new DataTable(PiPlatFormAddInTable.TableName);

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 依條件獲取資料列表
                    PiPlatFormAddInManager manager = new PiPlatFormAddInManager(dbProvider, userInfo);
                    dataTable = manager.GetDT(names, values);
                    dataTable.TableName = PiPlatFormAddInTable.TableName;
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 寫入調試訊息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return dataTable;
        }        

        /// <summary>
        /// 单个删除平台插件
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">主鍵</param>
        /// <returns>受影响的行数</returns>
        public int Delete(UserInfo userInfo, string id)
        {
            // 寫入調試訊息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加強安全驗證防止未授權匿名調用
            #if (!DEBUG)
				LogOnService.UserIsLogOn(userInfo);
            #endif

            int returnValue = 0;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPlatFormAddInManager manager = new PiPlatFormAddInManager(dbProvider, userInfo);
                    returnValue = manager.Delete(id);
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 寫入調試訊息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }

        /// <summary>
        /// 批量设置删除标志
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
        /// <returns>受影响的行数</returns>
        public int SetDeleted(UserInfo userInfo, string[] ids)
        {
            int returnValue = 0;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPlatFormAddInManager manager = new PiPlatFormAddInManager(dbProvider, userInfo);
                    returnValue = manager.SetDeleted(ids);
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
        }

        /// <summary>
        /// 设置可用性
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">主键</param>
        /// <param name="enabled">是否可用(1:可用、0：不可用)</param>
        /// <returns>受影响的行数</returns>
        public int SetEnabled(UserInfo userInfo, string id,int enabled)
        {
            int returnValue = 0;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPlatFormAddInManager manager = new PiPlatFormAddInManager(dbProvider, userInfo);
                    returnValue = manager.SetProperty(id,PiPlatFormAddInTable.FieldEnabled,enabled);
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return returnValue;
        }

        public byte[] GetTheLatestAddIn(UserInfo userInfo,string Id)
        {
            DataTable dtAddIn = new DataTable(PiPlatFormAddInTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPlatFormAddInManager manager = new PiPlatFormAddInManager(dbProvider, userInfo);
                    string[] names = { };
                    dtAddIn = manager.GetDTById(Id);
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            return BusinessLogic.GetBinaryFormatData(dtAddIn);
        }
    }
}
